package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.AdditionalFlowYieldPlanDao;
import com.qdlc.p2p.dal.dto.AdditionalFlowYieldPlan;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.FlowProjectModel;
import com.qdlc.p2p.dal.model.FlowYieldPlanModel;

/**
 * 理财人待收计划DAO接口
 * @author wjx
 * @version 2.0
 * @since 2015年05月15日 11时37分40秒
 */
@Repository(value = "additionalFlowYieldPlanDao")
public class AdditionalFlowYieldPlanDaoImpl extends BaseDaoImpl<AdditionalFlowYieldPlan> implements AdditionalFlowYieldPlanDao {

    @SuppressWarnings("unchecked")
    @Override
    public AdditionalFlowYieldPlan getAdditionalFlowYieldPlanById(long id) {
        String jpql = "from AdditionalFlowYieldPlan where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<AdditionalFlowYieldPlan> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (AdditionalFlowYieldPlan) list.get(0);
        } else {
            return null;
        }
    }

	@Override
	public PageDataList<FlowYieldPlanModel> getYieldPlanList(long userId,
			FlowProjectModel model) {
		QueryParam param = QueryParam.getInstance();
		param.addParam("user.userId", userId);
		if (model.getQueryStatus() !=99) {
			param.addParam("status", model.getQueryStatus());
		}
		int size = model.getRows();
		int page = model.getPage();
		if (size != 0) {
	        param.addPage(page, size);
		} else {
		    param.addPage(page);
		}
		PageDataList<AdditionalFlowYieldPlan> yieldPage = findPageList(param);
		PageDataList<FlowYieldPlanModel> yieldModelPage = new PageDataList<FlowYieldPlanModel>();
		List<FlowYieldPlanModel> yieldList = new ArrayList<FlowYieldPlanModel>();
		for (AdditionalFlowYieldPlan yield : yieldPage.getList()) {
			FlowYieldPlanModel yieldModel =FlowYieldPlanModel.instance(yield);
			yieldModel.setBuyCopies(yield.getFlowInvestor().getBuyCopies());
			yieldModel.setBuyMoney(yield.getFlowInvestor().getBuyMoney());
			yieldModel.setProjectName(yield.getFlowProject().getName());
			yieldModel.setApr(yield.getFlowProject().getApr());
			yieldModel.setUuid(yield.getFlowProject().getUuid());
			yieldModel.setShowPeriod(yield.getPeriod()+1);
			yieldList.add(yieldModel);
		}
		yieldModelPage.setList(yieldList);
		yieldModelPage.setPage(yieldPage.getPage());
		return yieldModelPage;
	}

	@Override
	public Object[] getFlowCollectionByUserId(long userId) {
		StringBuffer buffer = new StringBuffer("select sum(waitMoney),sum(waitYields) from AdditionalFlowYieldPlan where status = 0 and user.userId = :userId ");
		Query query = em.createQuery(buffer.toString());
		query.setParameter("userId", userId);
		Object[] object = (Object[])query.getSingleResult();
		return object;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getFlowCapitalByUserAndDate(User user, String date) {
		String sql = "select DATE_FORMAT(wait_time,'%m-%d'),sum(wait_money) from " + Global.DB_PREFIX + "additional_flow_yield_plan where status = 0  and DATE_FORMAT(wait_time,'%Y-%m') = :timeDate and user_id = :userId group by wait_time ";
		Query q = em.createNativeQuery(sql).setParameter("timeDate", date).setParameter("userId", user.getUserId());
	    return q.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getFlowInterestByUserAndDate(User user, String date) {
		String sql = "select DATE_FORMAT(wait_time,'%m-%d'),sum(wait_yields) from " + Global.DB_PREFIX + "additional_flow_yield_plan where status = 0  and DATE_FORMAT(wait_time,'%Y-%m') = :timeDate and user_id = :userId group by wait_time ";
		Query q = em.createNativeQuery(sql).setParameter("timeDate", date).setParameter("userId", user.getUserId());
	    return q.getResultList();
	}
    
}
